package cn.wolfcode.web.controller;

import cn.wolfcode.domain.ClassInfo;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.ClassInfoQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IClassInfoService;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping(("/class"))
public class ClassinfoController {

    @Autowired
    private IClassInfoService classInfoService;
    @Autowired
    private IEmployeeService employeeService;
    @RequestMapping("/list")
    public String listAll(Model model, ClassInfoQueryObject qo){
        PageInfo<ClassInfo> pageResult = classInfoService.query(qo);
        model.addAttribute("pageResult",pageResult);
        List<Employee> employees = employeeService.listAll();
        model.addAttribute("employees",employees);
        model.addAttribute("qo",qo);
        return "classinfo/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            if (id!=null){
                classInfoService.delete(id);
            }
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(ClassInfo classInfo){
        try {
            if (classInfo.getId()!=null){
                classInfoService.update(classInfo);
            }else {
                classInfoService.save(classInfo);
            }
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }
}
